package com.relytosoft.mizar.analyze.structure.services.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.relytosoft.dubhe.common.core.base.page.PageQuery;
import com.relytosoft.dubhe.common.core.object.ResponseResult;
import com.relytosoft.dubhe.common.core.utils.CommonUtils;
import com.relytosoft.mizar.analyze.structure.api.pojo.dto.AyStructureTaskDto;
import com.relytosoft.mizar.analyze.structure.api.pojo.dto.AyStructureTaskUpdateDto;
import com.relytosoft.mizar.analyze.structure.api.pojo.dto.AyTaskIdDto;
import com.relytosoft.mizar.analyze.structure.api.pojo.query.AyStruMotorVehicleSearchQuery;
import com.relytosoft.mizar.analyze.structure.api.pojo.query.AyStruNonMotorVehicleSearchQuery;
import com.relytosoft.mizar.analyze.structure.api.pojo.query.AyStructurePersonSearchQuery;
import com.relytosoft.mizar.analyze.structure.api.pojo.query.AyStructureTaskQuery;
import com.relytosoft.mizar.analyze.structure.api.pojo.vo.AyBatchVo;
import com.relytosoft.mizar.analyze.structure.api.pojo.vo.AyStructureTaskVo;
import com.relytosoft.mizar.analyze.structure.services.service.AyStructureTaskService;
import com.relytosoft.mizar.analyze.structure.services.service.AyStructureTaskSonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @Author: zyc
 * @version: v1.0
 * @description: com.relytosoft.mizar.analyze.structure.services.controller
 * @Date: 2022/5/12 15:45
 */
@Api(tags = {"结构化分析任务"})
@RestController
@RequestMapping("/structure/task")
public class AyStructureTaskController {

    @Resource
    private AyStructureTaskService ayStructureTaskService;

    @Resource
    private AyStructureTaskSonService ayStructureTaskSonService;


    @ApiOperation("查询结构化分析任务列表")
    @GetMapping({"/list"})
    public ResponseResult<Map<String, Object>> select(@RequestParam("queryBodys") String bodys, @RequestParam("pageQuery") String query) {
        AyStructureTaskQuery ayStructureTaskQuery = JSON.parseObject(bodys, AyStructureTaskQuery.class);
        PageQuery pageQuery = JSON.parseObject(query, new TypeReference<PageQuery>() {
        });
        IPage<AyStructureTaskVo> page = ayStructureTaskService.selectAyStructureTaskPageList(pageQuery, ayStructureTaskQuery);
        return ResponseResult.ok(CommonUtils.getPageData(page));
    }


    @ApiOperation("修改结构化分析任务")
    @PutMapping({"/update"})
    public ResponseResult<Void> update(@RequestBody AyStructureTaskUpdateDto ayStructureTaskVo) {
        boolean result = ayStructureTaskService.updateAyStructureTask(ayStructureTaskVo);
        if (result) {
            return ResponseResult.ok("修改结构化任务成功");
        }
        return ResponseResult.fail("修改结构化任务失败");
    }


    @ApiOperation("创建结构化分析任务")
    @PostMapping({"/insert"})
    public ResponseResult<AyBatchVo> insert(@RequestBody AyStructureTaskDto ayStructureTaskDto) {
        AyBatchVo ayBatchVo = ayStructureTaskService.insertTaskByDto(ayStructureTaskDto);
        return ResponseResult.ok(ayBatchVo);
    }


    @ApiOperation("删除结构化分析任务")
    @PutMapping({"/delete"})
    public ResponseResult<Void> delete(@RequestBody AyTaskIdDto ayTaskIdDto) {
        if (ayStructureTaskService.deleteTaskByIds(ayTaskIdDto)) {
            return ResponseResult.ok("已删除符合条件的任务");
        }
        return ResponseResult.fail("没有符合删除条件的任务");
    }


    @ApiOperation("启动结构化分析任务")
    @PostMapping({"/start"})
    public ResponseResult<Void> start(@RequestBody AyTaskIdDto ayTaskIdDto) {
        if (ayStructureTaskService.startTaskByIds(ayTaskIdDto)) {
            return ResponseResult.ok("已启动符合条件的任务");
        }
        return ResponseResult.fail("没有符合启动条件的任务");
    }


    @ApiOperation("停止结构化分析任务")
    @PostMapping({"/stop"})
    public ResponseResult<Void> stop(@RequestBody AyTaskIdDto ayTaskIdDto) {
        if (ayStructureTaskService.stopTaskByIds(ayTaskIdDto)) {
            return ResponseResult.ok("已停止符合条件的任务");
        }
        return ResponseResult.fail("没有符合停止条件的任务");
    }


    @ApiOperation("结构化任务行人结果检索")
    @GetMapping("/person/search")
    public ResponseResult<JSONObject> personSearch(@RequestParam("queryBodys") String bodys, @RequestParam("pageQuery") String query) {
        PageQuery pageQuery = JSON.parseObject(query, new TypeReference<PageQuery>() {
        });
        AyStructurePersonSearchQuery ayStructurePersonSearchQuery = JSONObject.parseObject(bodys, AyStructurePersonSearchQuery.class);
        return ResponseResult.ok(ayStructureTaskService.personResultSearch(ayStructurePersonSearchQuery, pageQuery));
    }

    @ApiOperation("结构化任务机动车结果检索")
    @GetMapping("/motor/vehicle/search")
    public ResponseResult<JSONObject> motorVehicleSearch(@RequestParam("queryBodys") String bodys, @RequestParam("pageQuery") String query) {
        PageQuery pageQuery = JSON.parseObject(query, new TypeReference<PageQuery>() {
        });
        AyStruMotorVehicleSearchQuery ayStruMotorVehicleSearchQuery = JSONObject.parseObject(bodys, AyStruMotorVehicleSearchQuery.class);
        return ResponseResult.ok(ayStructureTaskService.motorVehicleResultSearch(ayStruMotorVehicleSearchQuery, pageQuery));
    }

    @ApiOperation("结构化任务非机动车结果检索")
    @GetMapping("/nonmotor/vehicle/search")
    public ResponseResult<JSONObject> nonMotorVehicleSearch(@RequestParam("queryBodys") String bodys, @RequestParam("pageQuery") String query) {
        PageQuery pageQuery = JSON.parseObject(query, new TypeReference<PageQuery>() {
        });
        AyStruNonMotorVehicleSearchQuery ayStruNonMotorVehicleSearchQuery = JSONObject.parseObject(bodys, AyStruNonMotorVehicleSearchQuery.class);
        return ResponseResult.ok(ayStructureTaskService.nonMotorVehicleResultSearch(ayStruNonMotorVehicleSearchQuery, pageQuery));
    }

    @ApiOperation("结构化综合检索")
    @GetMapping("/integrated/search")
    public ResponseResult<JSONObject> integratedSearch(@RequestParam("queryBodys") String bodys, @RequestParam("pageQuery") String query) {
        PageQuery pageQuery = JSON.parseObject(query, new TypeReference<PageQuery>() {
        });
        AyStruNonMotorVehicleSearchQuery integratedSearch = JSONObject.parseObject(bodys, AyStruNonMotorVehicleSearchQuery.class);
        return ResponseResult.ok(ayStructureTaskService.integratedSearch(integratedSearch, pageQuery));
    }
}

